using System;
using Godot;
using GoGameClient.script.cmd;
using GoGameClient.script.dialog;
using GoGameClient.script.global;
using GoGameClient.script.network.webSocket;
using GoGameClient.script.resources;
using GoGameClient.script.role;
using Script.Message.Res;

namespace GoGameClient.script.node.create;

public partial class CreateRoleGui : Control
{
    // Called when the node enters the scene tree for the first time.


    private string _scenePath = "res://scene/SelectRole.tscn";

    //写个数组，存储三个图片路径的角色
    private string[] _rolePath =
    {
        "ren",
        "mo",
        "xian"
    };

    //记录当前选择的图片路径
    private string _currentButton = "ren";
    private TextureButton _upButton;
    private TextureButton _downButton;
    private TextureButton _selectButton;
    private TextureRect _background;
    private RoleSelectLayout _roleSelectLayout;
    private string _roleName;
    private Label _content;

    private Model _model;

    //方向
    private string[] _directions = { "上", "下", "左", "右", "左上", "左下", "右上", "右下" };
    private string[] _fightDirections = { "左上", "左下", "右上", "右下" };
    private string _currentDirection = "下";

    private string _currentAction = "站立";
    private LineEdit _nameInput;

    //这是个点击事件按钮，点击后，会触发这个方法	
    private void UpButtonPressed()
    {
        //根据当前图片路径，从数组中找到上一个图片的索引
        var index = Array.IndexOf(_rolePath, _currentButton) - 1;
        if (index < 0)
        {
            index = _rolePath.Length - 1;
        }

        _currentButton = _rolePath[index];
        SetSelectIcon(_currentButton);
        SetButtonIcon(index);
        SetBackground(index);
        SetRoleSelectLayout(index);
    }

    private void ReturnButtonPressed()
    {
        if (ResourceLoader.LoadThreadedGetStatus(_scenePath) != ResourceLoader.ThreadLoadStatus.Loaded)
        {
            return;
        }

        GetTree().ChangeSceneToPacked((PackedScene)ResourceLoader.LoadThreadedGet(_scenePath));
    }


    private void RandomButtonPressed()
    {
        _nameInput.Text = ResourcesControl.Instance().GetRandomName();
    }

    private void RoleSelect(string roleName)
    {
        _roleName = roleName;
        SetRoleModel(_roleName);
        IntroduceButtonPressed();
    }


    private void LeftButtonPressed()
    {
        int index;
        var directions = _currentAction.Equals("站立") ? _directions : _fightDirections;
        if (Array.IndexOf(directions, _currentDirection) == -1)
        {
            index = 0;
        }
        else
        {
            index = Array.IndexOf(directions, _currentDirection) - 1;
        }

        if (index < 0)
        {
            index = directions.Length - 1;
        }

        _currentDirection = directions[index];
        SetRoleAction();
    }

    private void RightButtonPressed()
    {
        int index;
        //根据_currentDirection查找索引
        var directions = _currentAction.Equals("站立") ? _directions : _fightDirections;
        if (Array.IndexOf(directions, _currentDirection) == -1)
        {
            index = 0;
        }
        else
        {
            index = Array.IndexOf(directions, _currentDirection) + 1;
        }

        if (index >= directions.Length)
        {
            index = 0;
        }

        _currentDirection = directions[index];
        SetRoleAction();
    }

    private void SetRoleAction()
    {
        _model.SetAction($"{_currentAction}-{_currentDirection}");
    }


    private void StandButtonPressed()
    {
        _currentAction = "站立";
        SetRoleAction();
    }

    private void RunButtonPressed()
    {
        _currentAction = "逃跑";
        SetRoleAction();
    }

    private void AttackButtonPressed()
    {
        _currentAction = "攻击";
        SetRoleAction();
    }

    private void MagicButtonPressed()
    {
        _currentAction = "施法";
        SetRoleAction();
    }


    private void OptionalSectButtonPressed()
    {
        TipsDialogUtil.ShowTips(this, "消息");
        if (_content == null)
        {
            return;
        }

        _content.Text = $"可加入门派 {ResourcesControl.Instance().GetRoleResources().GetRole(_roleName).JoinSects}";
    }

    private void OptionalWeaponButtonPressed()
    {
        if (_content == null)
        {
            return;
        }

        _content.Text = $"可使用武器 {ResourcesControl.Instance().GetRoleResources().GetRole(_roleName).UseWeapon}";
    }


    private void CreateButtonPressed()
    {
        if (_nameInput.Text.Equals(""))
        {
            TipsDialogUtil.ShowTips(this, "请输入角色名称");
            return;
        }

        var socketRequestConfig = new BodyRequestConfig<CreateRoleReq, SuccessRes>
        {
            Title = "创建角色",
            Cmd = (int)Router.Role,
            CmdMethod = (int)RoleRouter.Create,
            Data = new CreateRoleReq
            {
                Name = _nameInput.Text,
                Role = _roleName
            },
            Result = message =>
            {
                TipsDialogUtil.ShowTips(this, $"创建角色{message.Message}");
                ReturnButtonPressed();
            }
        };
        SocketControl.Instance().Request(socketRequestConfig);
    }

    private void IntroduceButtonPressed()
    {
        if (_content == null)
        {
            return;
        }

        _content.Text = ResourcesControl.Instance().GetRoleResources().GetRole(_roleName).Introduce;
    }


    private void SetRoleSelectLayout(int index)
    {
        switch (_rolePath[index])
        {
            case "xian":
                _roleSelectLayout.SetRace(RoleResources.Race.Xian);
                break;
            case "mo":
                _roleSelectLayout.SetRace(RoleResources.Race.Mo);
                break;
            case "ren":
                _roleSelectLayout.SetRace(RoleResources.Race.Ren);
                break;
        }
    }

    private void SetBackground(int index)
    {
        _background.Texture =
            (Texture2D)GD.Load($"res://resources/Ui/common/{_rolePath[index]}-background.png");
    }

    private void SetButtonIcon(int index)
    {
        var count = index - 1;
        if (count < 0)
        {
            count = _rolePath.Length - 1;
        }

        _upButton.TextureNormal =
            (AtlasTexture)GD.Load($"res://resources/Ui/button/{_rolePath[count]}-button/atlas_normal.tres");
        _upButton.TexturePressed =
            (AtlasTexture)GD.Load($"res://resources/Ui/button/{_rolePath[count]}-button/atlas_pressed.tres");
        _upButton.TextureHover =
            (AtlasTexture)GD.Load($"res://resources/Ui/button/{_rolePath[count]}-button/atlas_hover.tres");

        count = index + 1;
        if (count >= _rolePath.Length)
        {
            count = 0;
        }

        _downButton.TextureNormal =
            (AtlasTexture)GD.Load($"res://resources/Ui/button/{_rolePath[count]}-button/atlas_normal.tres");
        _downButton.TexturePressed =
            (AtlasTexture)GD.Load($"res://resources/Ui/button/{_rolePath[count]}-button/atlas_pressed.tres");
        _downButton.TextureHover =
            (AtlasTexture)GD.Load($"res://resources/Ui/button/{_rolePath[count]}-button/atlas_hover.tres");
    }


    private void SetSelectIcon(string currentButton)
    {
        _selectButton.TextureNormal =
            (AtlasTexture)GD.Load($"res://resources/Ui/button/{currentButton}-button-select/atlas_normal.tres");
        _selectButton.TexturePressed =
            (AtlasTexture)GD.Load($"res://resources/Ui/button/{currentButton}-button-select/atlas_pressed.tres");
        _selectButton.TextureHover =
            (AtlasTexture)GD.Load($"res://resources/Ui/button/{currentButton}-button-select/atlas_hover.tres");
    }

    private void DownButtonPressed()
    {
        // 根据当前图片索引，从数组中找到上一个图片的索引
        var index = Array.IndexOf(_rolePath, _currentButton) + 1;
        if (index >= _rolePath.Length)
        {
            index = 0;
        }

        _currentButton = _rolePath[index];
        SetSelectIcon(_currentButton);
        SetButtonIcon(index);
        SetBackground(index);
        SetRoleSelectLayout(index);
    }

    private void SetRoleModel(string name)
    {
        _model?.SetModel(name);
    }


    public override void _Ready()
    {
        ResourceLoader.LoadThreadedRequest(_scenePath);
        _content = GetNode<Label>("Content");
        IntroduceButtonPressed();
        _nameInput = GetNode<LineEdit>("NameInput");
        _model = GetNode<Model>("SelectRoleControl/Model");
        _background = GetNode<TextureRect>("Background");
        _roleSelectLayout = GetNode<RoleSelectLayout>("RoleSelectLayout");
        _upButton = GetNode<TextureButton>("SelectRaceControl/UpButton");
        _downButton = GetNode<TextureButton>("SelectRaceControl/DownButton");
        _selectButton = GetNode<TextureButton>("SelectRaceControl/SelectButton");
    }


    // Called every frame. 'delta' is the elapsed time since the previous frame.
    public override void _Process(double delta)
    {
    }
}